package com.fitz.provider.config;

import com.fitz.enums.SerializerEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author fitz
 * Created  on 2020/6/4
 */

@Deprecated
@Slf4j
@Configuration
@EnableConfigurationProperties(RpcProviderConfig.class)
public class RpcProviderAutoConfig {

    @Autowired
    private RpcProviderConfig rpcProviderConfig;

    @Bean
    public ProviderInitializer initializer(){
        SerializerEnum serializer = null;
        if (StringUtils.isBlank(rpcProviderConfig.getSerializerType())){
            serializer = SerializerEnum.HESSIAN2;
        }else {
            serializer = SerializerEnum.find(rpcProviderConfig.getSerializerType());
            if (serializer == null){
                serializer = SerializerEnum.HESSIAN2;
            }
        }
        log.info("Serializer type:{}",serializer.getType());
        return new ProviderInitializer(rpcProviderConfig.getServiceName(),rpcProviderConfig.getServerPort(),
                rpcProviderConfig.getRegistryAddress(), serializer.getCode());
    }
}
